<?php

/**
 * statistike actions.
 *
 * @package    dnevnik
 * @subpackage statistike
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class statistikeActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeOcene(sfWebRequest $request)
  {
     $statistike = new Statistike($this->getUser()->getAttribute('skolas'));
     if($request->getParameter('tip') == '')
     {
       $this->skola_lists = $statistike->skolaList();
     }

     else if($request->getParameter('tip') == 'skola')
     {
        list($this->predmets, $this->ocenas) = $statistike->predmetList($request->getParameter('skola_id'));

        return 'Skola';
     }

     else if($request->getParameter('tip') == 'razred')
     {
        list($this->razreds, $this->ocenas, $this->izostanaks, $this->uceniks) = $statistike->razredList($request->getParameter('skola_id'));

        return 'Razred';
     }

     else if($request->getParameter('tip') == 'razredrazred')
     {
        list($this->uceniks, $this->ocenas) = $statistike->razredRazred($request->getParameter('skola_id'), $request->getParameter('razred_id'));

        return 'RazredRazred';
     }

     else if($request->getParameter('tip') == 'razredpredmet')
     {
        list($this->predmets, $this->ocenas) = $statistike->razredPredmet($request->getParameter('skola_id'), $request->getParameter('razred_id'));

        return 'RazredPredmet';
     }
  }

  public function executeUnos(sfWebRequest $request)
  {
     $skolas = $this->getUser()->getAttribute('skolas');

     $statistike = new Statistike($skolas);

     $i = 0;
     $mesecs = array();
     while($i++ < 12)
       $mesecs[$i] = $i;
     $tr_god = date('Y');
     $i = 2008;
     while($i++ < $tr_god)
       $godinas[$i] = $i;

     $datums = array();
     if($this->getRequest()->isMethod('post'))
     {
        $num = cal_days_in_month(CAL_GREGORIAN, $request->getParameter('mesec'), $request->getParameter('godina'));
        $i = 0;
        while($i++ < $num)
          $datums[] = ($i<10)?"0$i":"$i";
        list($this->ocenas, $this->izostanaks, $this->domacis, $this->obavestenjes) = $statistike->skolaUnos($request->getParameter('skola_id'), $request->getParameter('mesec'), $request->getParameter('godina'));
     }

     else
     {
        $num = cal_days_in_month(CAL_GREGORIAN, date('n'), date('Y'));
        $i = 0;
        while($i++ < $num)
          $datums[] = ($i<10)?"0$i":"$i";
        list($this->ocenas, $this->izostanaks, $this->domacis, $this->obavestenjes) = $statistike->skolaUnos(key($skolas), date('n'), date('Y'));
     }

     $skola_list = array();
     $skola_dbs = DbFinder::from('Skola')->where('Id', 'in', array_keys($skolas))->find();
     foreach($skola_dbs as $skola_db)
       $skola_list[$skola_db->getId()] = $skola_db->getIme().' - '.$skola_db->getMesto();

     $this->skola_list = $skola_list;
     $this->godinas = $godinas;
     $this->mesecs = $mesecs;
     $this->datums = $datums;
  }

  public function executeLogiranje(sfWebRequest $request)
  {
     $skolas = $this->getUser()->getAttribute('skolas');
     $statistike = new Statistike($skolas);

     if($request->getParameter('tip'))
       $this->getUser()->setAttribute('log_search_tip', $request->getParameter('tip'));
     if($request->getParameter('username'))
       $this->getUser()->setAttribute('log_search_username', $request->getParameter('username'));
     if($request->getParameter('username') === '')
       $this->getUser()->setAttribute('log_search_username', null);

     list($this->logiranjes, $this->broj) = $statistike->logiranje($this->getUser()->getAttribute('log_search_tip'), $this->getUser()->getAttribute('log_search_username'), $request->getParameter('strana'));

     $this->profil_tips = array('' => 'Izaberi', 3 => 'Profesor', 4 => 'Roditelj', 5 => 'Učenik', 7 => 'Operater');
     $this->skolas = $skolas;
  }

  public function executeClanarine(sfWebRequest $request)
  {
     $skolas = $this->getUser()->getAttribute('skolas');
     $statistike = new Statistike($skolas);

     if($this->getRequest()->isMethod('post'))
       $skola_id = $request->getParameter('skola_id');
     else
     {
       $skola_ids = array_keys($skolas);
       $skola_id = $skola_ids[0];
     }

     $skola_list = array();
     $skola_dbs = DbFinder::from('Skola')->where('Id', 'in', array_keys($skolas))->find();
     foreach($skola_dbs as $skola_db)
       $skola_list[$skola_db->getId()] = $skola_db->getIme().' - '.$skola_db->getMesto();

     $this->skola_list = $skola_list;
     list($this->razreds, $this->clanarinas, $this->kredits, $this->tst_socs, $this->clanarina_br, $this->kredit_br, $this->tst_soc_br, $this->ukupna_suma) = $statistike->clanarina($skola_id);
  }

  public function executeClanarineRazred(sfWebRequest $request)
  {
     $skolas = $this->getUser()->getAttribute('skolas');
     $statistike = new Statistike($skolas);

     $this->uceniks = $statistike->clanarinaRazred($request->getParameter('razred_id'));
  }

  public function executeZakljucne(sfWebRequest $request)
  {
     $period = $request->getParameter('period');
     $razred_smer_dbs = DbFinder::from('Razred')->join('Smer')->select(array('Razred.Id', 'Smer.Naziv'))->where('Razred.SkolaId', $request->getParameter('skola_id'))->where('Smer.Tip', '1')->orderBy('Razred.Razred')->orderBy('Razred.Slovo')->find();
     $razred_ignores = array();
     foreach($razred_smer_dbs as $razred_smer_db)
       $razred_ignores[$razred_smer_db['Razred.Id']] = Statistike::$ignore_predmeti[$razred_smer_db['Smer.Naziv']];
     $razred_podatak_dbs = DbFinder::from('Razred')->leftJoin('Razred.Id', 'Ucenik.RazredId')->select(array('Razred.Id', 'Razred', 'Slovo', 'brUcenika'))->withColumn('count(Ucenik.Id)', 'brUcenika')->where('Razred.SkolaId', $request->getParameter('skola_id'))->where('Smer.Tip', '1')->groupBy('Razred.Id')->orderBy('Razred.Razred')->orderBy('Razred.Slovo')->find();
     $razred_podataks = array();
     $ukupno_ucenika = 0;
     foreach($razred_podatak_dbs as $razred_podatak_db)
     {
        $ukupno_ucenika += $razred_podatak_db['brUcenika'];
        $razred_podataks[$razred_podatak_db['Razred.Id']] = array(RazredPeer::razredRimski($razred_podatak_db['Razred']).' - '.$razred_podatak_db['Slovo'], $razred_podatak_db['brUcenika']);
     }

     $zakljucna_dbs = DbFinder::from('Zakljucna')->leftJoin('Zakljucna.UcenikId', 'Ucenik.Id')->select(array('Ucenik.RazredId', 'UcenikId', 'PredmetId', 'Ocena'))->where('Zakljucna.Period', '1')->where('Ucenik.SkolaId', $request->getParameter('skola_id'))->find();
     $zakljucnas = array();
     foreach($zakljucna_dbs as $zakljucna_db)
     {
        $zakljucnas[$zakljucna_db['Ucenik.RazredId']][$zakljucna_db['UcenikId']][$zakljucna_db['PredmetId']] = $zakljucna_db['Ocena'];
     }

     $this->proseks = array();
     $this->number_razred = array();
     $number_all = array();
     $number_razreds = array();
     $string_all = array();
     foreach($razred_ignores as $razred_id => $ignore)
     {
        $this->proseks[$razred_id] = array('neocenjen' => 0, '1kec' => 0, '2kec' => 0, '3kec' => 0, 'odlican' => 0, 'vrlodobar' => 0, 'dobar' => 0, 'dovoljan' => 0);
        $this->number_razred[$razred_id] = 0;
        if(isset($zakljucnas[$razred_id]))
        {
           foreach($zakljucnas[$razred_id] as $zakljucna_ucenik)
           {
              $pr_arr = Statistike::prosek($zakljucna_ucenik, $ignore);
              if($pr_arr[0])
              {
                 $number_all[] = $pr_arr[0];
                 $number_razreds[$razred_id][] = $pr_arr[0];
                 $string_all[] = $pr_arr[1];
              }
              $this->proseks[$razred_id][$pr_arr[1]] ++;
           }
        }

        $this->proseks[$razred_id]['neocenjen'] += $razred_podataks[$razred_id][1] - array_sum($this->proseks[$razred_id]);
        $this->razred_podataks = $razred_podataks;
     }

     foreach($number_razreds as $razred_id => $number_razred)
     {
        $this->number_razred[$razred_id] = array_sum($number_razred) / count($number_razred);
     }
     arsort($this->number_razred);

     $this->prosek_all = number_format(array_sum($number_all) / count($number_all), 2);
     $this->string_all = array_count_values($string_all);
     if(!isset($this->string_all['odlican'])) $this->string_all['odlican'] = 0;
     if(!isset($this->string_all['vrlodobar'])) $this->string_all['vrlodobar'] = 0;
     if(!isset($this->string_all['dobar'])) $this->string_all['dobar'] = 0;
     if(!isset($this->string_all['dovoljan'])) $this->string_all['dovoljan'] = 0;
     if(!isset($this->string_all['1kec'])) $this->string_all['1kec'] = 0;
     if(!isset($this->string_all['2kec'])) $this->string_all['2kec'] = 0;
     if(!isset($this->string_all['3kec'])) $this->string_all['3kec'] = 0;
     $this->string_all['neocenjen'] = $ukupno_ucenika - count($number_all);
     $this->ukupno_ucenika = $ukupno_ucenika;

     // izostanci
     $this->neopr = DbFinder::from('Izostanak')->join('Razred')->select('neopr')->withColumn('count(Izostanak.Id)', 'neopr')->where('Razred.SkolaId', $request->getParameter('skola_id'))->where('Opravdan', '0')->findOne();
     $this->opr = DbFinder::from('Izostanak')->join('Razred')->select('opr')->withColumn('count(Izostanak.Id)', 'opr')->where('Razred.SkolaId', $request->getParameter('skola_id'))->where('Opravdan', '1')->findOne();

     if($request->getParameter('print_verzija'))
       $this->setLayout('tabelarni');
  }

}